package com.xc.ezplan.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xc.ezplan.admin.entity.AdminAliConfig;
import com.xc.ezplan.admin.service.AdminAliConfigService;
import com.xc.ezplan.admin.mapper.AdminAliConfigMapper;
import com.xc.ezplan.admin.vo.AdminAliConfigVo;
import com.xc.ezplan.cache.constants.RedisKey;
import com.xc.ezplan.cache.utils.CacheUtil;
import com.xc.ezplan.core.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lixingchen
 * @date 2022/12/04
 * @description 阿里云配置实现类
 */
@Service
@Primary
@RequiredArgsConstructor
@CacheConfig(cacheNames = "aliConfig")
public class AdminAliConfigServiceImpl extends ServiceImpl<AdminAliConfigMapper, AdminAliConfig>
    implements AdminAliConfigService{

    private final AdminAliConfigMapper adminAliConfigMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createConfig(AdminAliConfigVo adminAliConfigVo) {
        AdminAliConfig adminAliConfig = new AdminAliConfig();
        BeanUtils.copyProperties(adminAliConfigVo,adminAliConfig);
        adminAliConfigMapper.insert(adminAliConfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteConfig(List<Long> ids) {
        adminAliConfigMapper.deleteBatchIds(ids);
        CacheUtil.deleteCache(RedisKey.ALI_CONFIG_ID,ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateConfig(AdminAliConfigVo adminAliConfigVo) {
        AdminAliConfig adminAliConfig = new AdminAliConfig();
        BeanUtils.copyProperties(adminAliConfigVo,adminAliConfig);
        adminAliConfigMapper.update(adminAliConfig,new UpdateWrapper<AdminAliConfig>().lambda().
                eq(AdminAliConfig::getId,adminAliConfig.getId()));
        CacheUtil.deleteCache(RedisKey.ALI_CONFIG_ID,adminAliConfigVo.getId());
    }

    @Override
    public Object queryAllConfig(String keyword) {
        LambdaQueryWrapper<AdminAliConfig> queryWrapper = new QueryWrapper<AdminAliConfig>().lambda().like(
                StringUtils.isNotBlank(keyword), AdminAliConfig::getBucket,
                keyword);
        return adminAliConfigMapper.selectList(queryWrapper);
    }

    @Override
    @Cacheable(key = "'id:' + #p0")
    public Object queryConfig(Long id) {
        return adminAliConfigMapper.selectById(id);
    }
}




